package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.mapper.SystemConfigMapper;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.pojo.SystemConfig;
import com.wty.cmims.service.SystemConfigService;
import org.springframework.stereotype.Service;

/**
* @author wty
* @description 针对表【system_config】的数据库操作Service实现
* @createDate 2025-02-27 13:09:57
*/
@Service
public class SystemConfigServiceImpl extends ServiceImpl<SystemConfigMapper, SystemConfig>
    implements SystemConfigService {

    @Override
    public ResponseResult<String> findAnnouncement() {
        ResponseResult<String> responseResult = new ResponseResult<>();
        // 查询announcement字段的值
        SystemConfig systemConfig = this.getOne(new QueryWrapper<SystemConfig>().select("announcement"));
        if (systemConfig != null){
            responseResult.setData(systemConfig.getAnnouncement());
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询系统公告成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询系统公告失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> updateAnnouncement(String announcement) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        SystemConfig systemConfig = this.getOne(new QueryWrapper<SystemConfig>().select("announcement"));
        if (systemConfig != null){
            systemConfig.setId(1);
            systemConfig.setAnnouncement(announcement);
            boolean update = this.updateById(systemConfig);
            if (update){
                responseResult.setCode(Code.SUCCESS);
                responseResult.setMessage("修改系统公告成功");
            } else {
                responseResult.setCode(Code.ERROR);
                responseResult.setMessage("修改系统公告失败");
            }
        }
        return responseResult;
    }
}




